﻿/****************************************************************************************
 * Copyright (c) 2010~2012 All Rights Reserved by
 *  G-Net Integrated Service Co.,  Ltd. 
 ****************************************************************************************/
/**
 * @file ConferenceOpenHelper.java
 * @author wenhui.li
 * @date 2012-9-26 下午8:00:22 
 * Revision History 
 *     - 2012-9-26: change content for what reason
 ****************************************************************************************/

package com.kingtone.rongji.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.kingtone.rongji.tool.AppContasts;

/**
 * @brief 创建ConferenceOpenHelper类
 */
public class DataBaseOpenHelper extends SQLiteOpenHelper {

	private static String mName = AppContasts.DATABASE_NAME;;
    private static  int mNewVersion = AppContasts.DATABASE_VERSION;;
	/**
	 * @brief 构造函数
	 * @param context 上下文
	 */
	public DataBaseOpenHelper(Context context) 
	{
		super(context, mName, null, mNewVersion);
	} 

	/**
	 * @brief 创建表
	 * @param  db 数据库对象
	 */
	@Override
	public void onCreate(SQLiteDatabase db)
	{
		    //创建Province 表
			db.execSQL("CREATE TABLE IF NOT EXISTS "    
				+ AppContasts.PROVINCE_TABLE + " ("    
				+AppContasts.ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
				+ AppContasts.REGION_NAME + " VARCHAR,"
				+ AppContasts.REGION_ID + " INTEGER )");  
			
			//创建City 表
			db.execSQL("CREATE TABLE IF NOT EXISTS "    
					+ AppContasts.CITY_TABLE + " ("    
					+ AppContasts.ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
					+ AppContasts.REGION_NAME + " VARCHAR,"
					+ AppContasts.REGION_ID + " INTEGER,"
					+ AppContasts.PARENT_ID + " INTEGER )"); 
			
			//创建County 表
			db.execSQL("CREATE TABLE IF NOT EXISTS "    
					+ AppContasts.COUNTY_TABLE + " ("    
					+ AppContasts.ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
					+ AppContasts.REGION_NAME + " VARCHAR,"
					+ AppContasts.REGION_ID + " INTEGER,"  
					+ AppContasts.PARENT_ID + " INTEGER )"); 
	}
	

	/**
	 * @brief 数据库版本更新
	 * @param db 数据库对象
	 * @param oldVersion 旧版本号
	 * @param newVersion 新版本号
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
	{
	     onCreate(db); 
	}
	
	/**
	 * 数据库降级时调用
	 * @param db 数据库对象
	 * @param oldVersion 数据库旧版本号
	 * @param newVersion 数据库新版本号
	 */
	
	public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		try
        {
		     db.execSQL("DROP TABLE IF EXISTS "+ AppContasts.PROVINCE_TABLE);   
		     onCreate(db);
		     
		     db.execSQL("DROP TABLE IF EXISTS "+ AppContasts.CITY_TABLE);   
		     onCreate(db);
		     
		     db.execSQL("DROP TABLE IF EXISTS "+ AppContasts.COUNTY_TABLE);   
		     onCreate(db);
		    
        }catch(Exception e)
        {
        	e.printStackTrace();
        }     
	}
	
	/**
	 * @brief
	 * @param db
	 * @return
	 */
	public boolean isDbNotLock(SQLiteDatabase db)
	{
		while (db.isDbLockedByOtherThreads()) 
		{
			Log.w("dblock", "db is locked by other threads!");
			try 
			{
				Thread.sleep(10);
			} catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
		}
		return true;
	}
}

